/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    usart.h
  * @brief   This file contains all the function prototypes for
  *          the usart.c file
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __USART_H__
#define __USART_H__

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* USER CODE BEGIN Includes */
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#define UART1_BUFF_SIZE 256  /*! uart1 rx buff size define */
#define UART2_BUFF_SIZE 1024 /*! uart2 rx buff size define */
#define UART3_BUFF_SIZE 256  /*! uart3 rx buff size define */

#define NB_UART    huart1 /*! nb */
#define NB_UART_RX g_uart1Rx

#define RS485_UART    huart2
#define RS485_UART_RX g_uart2Rx

#define DEBUG_UART    huart3 /*! debug uart define */
#define DEBUG_UART_RX g_uart3Rx

/* USER CODE END Includes */

extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;
extern UART_HandleTypeDef huart3;

/* USER CODE BEGIN Private defines */
#pragma pack(1)
typedef struct {
    volatile uint8_t  flag;
    volatile uint16_t len;
    uint8_t*          buff;
} usartRecvTypedef_t; /*! define a usart rx struct */
#pragma pack()
extern char               printfBuff[512];
extern usartRecvTypedef_t g_uart1Rx; // debug lpuart1 115200
extern usartRecvTypedef_t g_uart2Rx; // nb usart1  9600
extern usartRecvTypedef_t g_uart3Rx; // rs485 usart2 4800
/* USER CODE END Includes */

extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;
extern UART_HandleTypeDef huart3;

/* USER CODE BEGIN Private defines */

/* USER CODE END Private defines */

void MX_USART1_UART_Init(void);
void MX_USART2_UART_Init(void);
void MX_USART3_UART_Init(void);

/* USER CODE BEGIN Prototypes */

void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle);

void usart_l_rcvd_idle(void); /*! uart rx done callback interface */
void usart_2_rcvd_idle(void);
void usart_3_rcvd_idle(void);

void usart_send_byte(UART_HandleTypeDef* Huart, uint8_t data);
void usart_send_data(UART_HandleTypeDef* Huart, uint8_t* buff, uint16_t size);
void usart_send_string(UART_HandleTypeDef* Huart, char* str);

void usart_rx_buff_clear(usartRecvTypedef_t* uart_rx);

void debug_send_string(char* str);
void msg_printf(char* fmt, ...); /*! uart debug interface */
void msg_printf_asc(uint8_t* pdata, uint16_t length);
void msg_printf_hex(uint8_t* pdata, uint16_t length);
/* USER CODE END Prototypes */

#ifdef __cplusplus
}
#endif

#endif /* __USART_H__ */

